/// <remarks>
/// Logger Class
/// This is a simple logger class using the Singleton pattern.
/// 
/// Author: Georges Marceau, Matricis Informatique Inc.
/// Co-author :Louis-Philippe Lebouthillier
/// </remarks>
/// <example>
/// <code>
/// // instantiate the Class
/// private Logger logger = Logger.Instance;
/// // Initialize the logger
/// this.logger.TextBoxLogger = myOutputRichTextBox;
/// // use the logger
/// this.logger.Log("My message");
/// </code>
/// </example>

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace VcGUI
{
    public sealed class Logger
    {
        private static Logger instance = null;
        private static readonly object padlock = new object();
        System.Windows.Forms.RichTextBox txtBoxlogger = null;

        public static Logger Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Logger();
                    }
                    return instance;
                }
            }
        }

        Logger()
        {
            Console.WriteLine("Logger initialized");
        }

        /// <summary>
        /// Log Method
        /// </summary>
        /// <param name="msg"></param>
        public void Log(string msg)
        {
            if (txtBoxlogger != null)
            {
                txtBoxlogger.AppendText(System.DateTime.Now + " " + msg + '\n');
                txtBoxlogger.Focus();
                txtBoxlogger.SelectionStart = txtBoxlogger.Text.Length;
                txtBoxlogger.ScrollToCaret();
            }
            else
                Console.WriteLine("Logger not initialized");
        }

        private void DefaultColor()
        {
            txtBoxlogger.SelectionColor = System.Drawing.Color.Black;
        }

        //overload
        /// <summary>
        /// Log Method with custom colored output
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="color"></param>
        private void Log(string msg, System.Drawing.Color color)
        {
            if (txtBoxlogger != null)
            {
                txtBoxlogger.SelectionColor = color;
                txtBoxlogger.AppendText(System.DateTime.Now + " " + msg + '\n');
                txtBoxlogger.Focus();
                txtBoxlogger.SelectionStart = txtBoxlogger.Text.Length;
                txtBoxlogger.ScrollToCaret();
                DefaultColor();
            }
            else
                Console.WriteLine("Logger not initialized");
        }

        /// <summary>
        /// TextBoxLogger Property
        /// Initialize the logger and set the RichTextBox to use
        /// </summary>
        /// <param name="TextBoxLogger"></param>
        public System.Windows.Forms.RichTextBox TextBoxLogger
        {
            set
            {
                txtBoxlogger = value;
            }
        }

        public void Error(string errorMsg)
        {
            Log(errorMsg, System.Drawing.Color.Red);
        }

        public void Warning(string warningMsg)
        {
            Log(warningMsg, System.Drawing.Color.Yellow);
        }

        public void Info(string infoMsg)
        {
            Log(infoMsg, System.Drawing.Color.Green);
        }

        public void ServerInfo(string serverInfoMsg)
        {
                Log(serverInfoMsg, System.Drawing.Color.Blue);
        }

        public void DebugInfo(string debugInfoMsg)
        {
                Log(debugInfoMsg, System.Drawing.Color.Black);
        }
    }
}